package org.tooyoung.fourone.chat;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;


import com.hyphenate.chat.EMConversation;
import com.hyphenate.easeui.ui.EaseConversationListFragment;

import org.tooyoung.fourone.BaseFragment;
import org.tooyoung.fourone.R;

/**
 * A simple {@link Fragment} subclass.
 */
public class ChatFragment extends BaseFragment implements EaseConversationListFragment.EaseConversationListItemClickListener, View.OnClickListener {

    public ChatFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View ret = inflater.inflate(R.layout.fragment_chat, container, false);

        ContactListFragment fragment = new ContactListFragment();
        fragment.setConversationListItemClickListener(this);

        ImageView addMenu = (ImageView) ret.findViewById(R.id.img_menu_add);
        ImageView contactMenu = (ImageView) ret.findViewById(R.id.img_menu_contact);
        addMenu.setOnClickListener(this);
        contactMenu.setOnClickListener(this);
        FragmentManager manager = getFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.replace(R.id.contact_list_container, fragment);
        transaction.commit();
        return ret;
    }

    @Override
    public String getTitle() {
        return "聊天";
    }

    @Override
    public void onListItemClicked(EMConversation conversation) {
        Intent intent = new Intent(getContext(), ChatActivity.class);
        String userName = conversation.getUserName();
        intent.putExtra("user", userName);
        startActivity(intent);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.img_menu_add:
                Intent intent = new Intent(getContext(), AddActivity.class);
                startActivity(intent);
                break;
            case R.id.img_menu_contact:
                intent = new Intent(getContext(), ContactListActivity.class);
                startActivity(intent);
                break;
        }
    }
}
